package com.atcjx.service;

import com.atcjx.MyUtils.File.FileUtils;
import com.atcjx.pojo.configPojo.UserConfig;
import com.atcjx.exception.UserException;
import com.atcjx.mapper.ArticleMapper;
import com.atcjx.mapper.CategoryMapper;
import com.atcjx.mapper.FileMapper;
import com.atcjx.pojo.Article.Article;
import com.atcjx.pojo.Query.Article.ArticleQueryPo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName FileServiceImpl.java
 * @Description TODO
 * @createTime 2021年10月10日 15:57:00
 */
@Service
@Slf4j
public class FileServiceImpl {

    @Autowired
    FileMapper fileMapper;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    CategoryMapper categoryMapper;

    // 获取用户配置
    @Autowired
    UserConfig userConfig;
    public Object getPic(int page,int limit){
        PageHelper.startPage(page,limit);
        List<String> pic = fileMapper.getPic();
        PageInfo pageInfo=new PageInfo(pic);
        return pageInfo;
    }

    public void deleteImgByURL(String url){
        fileMapper.deletePic(url);
    }
    public void openFile(String articleId) throws UserException {
        ArticleQueryPo queryPo = new ArticleQueryPo();
        queryPo.setArticleId(articleId);
        Article article = articleMapper.getArticlesByCondition(queryPo).get(0);
        FileUtils.openFile(article, userConfig.getLocalMdFileUploadPath());
    }
    public void updateArticleLocal() throws UserException {
        List<Article> articlesByCondition = articleMapper.getArticlesByCondition(new ArticleQueryPo());
        for(Article article:articlesByCondition){
            log.info("正在本地写入文件:{}",article.getTitle());
            FileUtils.updateArticleLocal(article, userConfig.getLocalMdFileUploadPath());
        }
    }
}
